const { Controller } = require("egg");
const Message = require("../utils/message");

/**
 * @Controller user-controller:登录相关接口
 */

class UserController extends Controller {
  /**
   * @Router post /user/login
   * @summary 登录
   * @description 身份认证
   * @request body loginRequest
   */
  async login () {
    const { ctx, service, app } = this
    const loginData = ctx.request.body
    let account = await service.user.login(loginData)
    // 签发token
    const token = app.jwt.sign(
      { ...account },
      app.config.jwt.secret,
      { expiresIn: '60s' }
    )
    // 响应结果
    ctx.body = new Message(200, '登录成功', { token })
  }
}

module.exports = UserController